package base.day08;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 描述：
 *      给定一个字符串类型的数组strs，找到一种拼接方式，使得所有字符串拼接起来之后形成的字符串具有最低的字典序
 * @author hl
 * @version 1.0
 * @date 2020/11/1 8:42
 */
public class JointStrs {

    public static class MyCompare implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            return (o1 + o2).compareTo(o2 + o1);
        }
    }

    public static String joinStrs(String[] strs){
        if(strs == null || strs.length == 0){
            return null;
        }
        Arrays.sort(strs, new MyCompare());
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < strs.length; i++) {
            res.append(strs[i]);
        }
        return res.toString();
    }
    public static void main(String[] args) {
        String[] strs1 = { "jibw", "ji", "jp", "bw", "jibw" };
        System.out.println(joinStrs(strs1));

        String[] strs2 = { "ba", "b" };
        System.out.println(joinStrs(strs2));

    }
}
